package com.example.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
@Configuration
public class redisUtil {
	   @Resource
	    ValueOperations<String, String> valOpsStr;

	    @Resource
	    HashOperations<String, String, Object> valOpsHash;
	    
	    @Resource
	    ListOperations<String, String> valOpsList;
	    
	    @Resource
	    SetOperations<String, String> valOpsSet;
	    
	    @Autowired
	    RedisTemplate<String, String> redisTemplate;
	    
	    @Resource
	    ValueOperations<String, String> valOpsObj;
	    /**
	     * 自增加1
	     * @param key
	     * @return
	     */
	    public void incrValue(String key){
	        valOpsStr.increment(key, 1);
	    }
	    
	    /**
	     * 根据指定key获取String
	     * @param key
	     * @return
	     */
	    public String getStr(String key){
	        return valOpsStr.get(key).toString();
	    }

	    /**
	     * 设置Str缓存
	     * @param key
	     * @param val
	     */
	    public void setStr(String key, String val){
	        valOpsStr.set(key,val);
	    }
	    
	    /**
	     * 设置Str缓存
	     * @param key
	     * @param val
	     */
	    public void setStr(String key, String val,String liveTime){
	        valOpsStr.set(key,val);
	        redisTemplate.expire(key, Integer.valueOf(liveTime), TimeUnit.SECONDS);
	    }
	    
	    /**
	     * 删除指定key
	     * @param key
	     */
	    public void del(String key){
	    	redisTemplate.delete(key);
	    }

	    /**
	     * 根据指定o获取Object
	     * @param o
	     * @return
	     */
	    public Object getObj(Object o){
	        return valOpsObj.get(o);
	    }

	    /**
	     * 设置obj缓存
	     * @param o1
	     * @param o2
	     */
	    public void setObj(String o1, String o2){
	        valOpsObj.set(o1, o2);
	    }

	    /**
	     * 删除Obj缓存
	     * @param o
	     */
	    public void delObj(String o){
	    	redisTemplate.delete(o);
	    }
	    
	    /**
	     * Hash操作
	     * @param o
	     */
	    public void HashOpertion(String key){
	    	Map<String,String> map=new HashMap<String,String>();  
	        map.put("key1","value1");  
	        map.put("key2","value2");  
	        map.put("key3","value3");  
	        map.put("key4","value4");  
	        map.put("key5","value5");  
	    	valOpsHash.putAll(key, map);
	    	String value = (String)valOpsHash.get(key, "key5");
	    	System.out.println(value);
	    	Map<String,Object> resultMap= valOpsHash.entries(key); 
	    	System.out.println(resultMap);
	    	String obj = "helloworld";
	    	valOpsHash.put(key, "key5", obj);
	    	resultMap= valOpsHash.entries(key); 
	    	System.out.println(resultMap);
	    	
	    }

	    /**
	     * List操作
	     * @param o
	     */
	    public void ListOpertion(String key){
	    	List<String> list1=new ArrayList<String>();  
	        list1.add("a1");  
	        list1.add("a2");  
	        list1.add("a3");  
	   
	        List<String> list2=new ArrayList<String>();  
	        list2.add("b1");  
	        list2.add("b2");  
	        list2.add("b3");
	        list2.add("b4");
	        list2.add("b5");
	        Long l = valOpsList.leftPushAll(key+1, list1);
	        Long ll = valOpsList.rightPushAll(key+2, list2);
	        System.out.println( l + "----" + ll);
	        
	        String str = (String) valOpsList.rightPopAndLeftPush(key+1, key+2);
	        System.out.println( str );
	    }
	    
	    /**
	     * Set操作
	     * @param o
	     */
	    public void SetOpertion(String key){
	    	Set<String>set1=new HashSet<String>();  
	        set1.add("set1");  
	        set1.add("set2");  
	        set1.add("set3");  
	        String[] ob = set1.toArray(new String[]{});
	        valOpsSet.add(key, ob);
	        Set<String> resultSet =valOpsSet.members(key);  
	        System.out.println("resultSet:"+resultSet); 
	    }
	    
	    /**
	     * 批量获取redis
	     * @param key
	     */
	    public void getStr_pipelined(final String key){
	    	long start2 = System.currentTimeMillis();
	    	List<Object> list = redisTemplate.execute(new RedisCallback<List<Object>>(){
	    		
				@Override
				public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
					//打开管道流
					connection.openPipeline();
					for (int i = 0; i < 100000; i++) {
						//使用connection操作
						connection.get((key+i).getBytes());
					}
					//关闭管道流
					return connection.closePipeline();
				}
	    	});
	    	long end2 = System.currentTimeMillis();
			System.out.println(end2 - start2);
			System.out.println(list);
	    }
	    
	    /**
	     * 批量设置redis
	     * @param key
	     */
	    public void setStr_pipelined(final String key){
	    	long start2 = System.currentTimeMillis();
	    	redisTemplate.execute(new RedisCallback<Long>(){

				@Override
				public Long doInRedis(RedisConnection connection) throws DataAccessException {
					//打开管道流
					connection.openPipeline();
					for (int i = 0; i < 100000; i++) {
						//使用connection操作
						connection.set((key+i).getBytes(),(key+i).getBytes());
					}
					//关闭管道流
					connection.closePipeline();
					return null;
				}
	    	});
	    	long end2 = System.currentTimeMillis();
			System.out.println(end2 - start2);
	    }
	    public void hashGet(String key){
	    	System.out.println( valOpsHash.entries(key));
	    	System.out.println( valOpsHash.values(key));
	    	System.out.println( valOpsHash.keys(key));
	    }
}
